package com.project.web.controller.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.project.client.domain.AreaInfoEntity;
import com.project.client.domain.CentigradeRecordEntity;
import com.project.client.service.AreaInfoService;
import com.project.client.service.CentigradeRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.project.common.annotation.Log;
import com.project.common.core.controller.BaseController;
import com.project.common.core.domain.AjaxResult;
import com.project.common.enums.BusinessType;
import com.project.common.utils.poi.ExcelUtil;
import com.project.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 温度记录数据Controller
 *
 * @author project
 * @date 2021-05-10
 */
@RestController
@RequestMapping("/client/centigrade")
public class CentigradeRecordController extends BaseController {
    @Autowired
    private CentigradeRecordService service;
    @Autowired
    private AreaInfoService areaInfoService;

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<CentigradeRecordEntity> util = new ExcelUtil<>(CentigradeRecordEntity.class);
        List<AreaInfoEntity> areaList = areaInfoService.selectByLevel(2l, 2l);
        List<CentigradeRecordEntity> list = new ArrayList<>();
        Date date = new Date();
        for (AreaInfoEntity area : areaList) {
            CentigradeRecordEntity entity = new CentigradeRecordEntity();
            entity.setAreaName(area.getAreaName());
            entity.setRecordDate(date);
            list.add(entity);
        }
        return util.exportExcel(list, "温度数据");
    }

    @Log(title = "温度数据", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('client:centigrade:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<CentigradeRecordEntity> util = new ExcelUtil<>(CentigradeRecordEntity.class);
        List<CentigradeRecordEntity> userList = util.importExcel(file.getInputStream());
        return AjaxResult.success(service.importData(userList));
    }

    /**
     * 查询温度记录数据列表
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:list')")
    @GetMapping("/list")
    public TableDataInfo list(CentigradeRecordEntity entity) {
        startPage();
        List<CentigradeRecordEntity> list = service.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出温度记录数据列表
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:export')")
    @Log(title = "温度数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CentigradeRecordEntity entity) {
        List<CentigradeRecordEntity> list = service.selectList(entity);
        ExcelUtil<CentigradeRecordEntity> util = new ExcelUtil<>(CentigradeRecordEntity.class);
        return util.exportExcel(list, "温度记录数据数据");
    }

    /**
     * 获取温度记录数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(service.selectById(id));
    }

    /**
     * 新增温度记录数据
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:add')")
    @Log(title = "温度数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated CentigradeRecordEntity entity) {
        return toAjax(service.save(entity));
    }

    /**
     * 修改温度记录数据
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:edit')")
    @Log(title = "温度数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated CentigradeRecordEntity entity) {
        return toAjax(service.update(entity));
    }

    /**
     * 删除温度记录数据
     */
    @PreAuthorize("@ss.hasPermi('client:centigrade:remove')")
    @Log(title = "温度数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(service.delete(ids));
    }
}